﻿using System.Windows;
using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Rio.Framework.Localisation;

namespace Rio.Framework.Pages.Workflow.Filters
{
    public partial class ReadOrNotControl : IFilteringControl
    {
        #region Fields

        /// <summary>
        /// Identifies the <see cref="IsActive"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty IsActiveProperty = 
            DependencyProperty.Register(
                "IsActive",
                typeof(bool),
                typeof(ReadOrNotControl),
                new PropertyMetadata(false));

        private GridViewBoundColumnBase column;
        private Telerik.Windows.Data.FilterDescriptor m_oFilter;
        private int m_oSelectedValue;

        #endregion Fields

        #region Constructors

        public ReadOrNotControl()
        {
            InitializeComponent();
            InitLabel();
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets or sets a value indicating whether the filtering is active.
        /// </summary>
        public bool IsActive
        {
            get { return (bool)GetValue(IsActiveProperty); }
            set { SetValue(IsActiveProperty, value); }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Translation
        /// </summary>
        public void InitLabel()
        {
            //Retrieves label.
            rRead.Content = Resx.GetLabel(ResxFlowPage.READ);
            rNotRead.Content = Resx.GetLabel(ResxFlowPage.NOTREAD);
            filterButton.Content = Resx.GetLabel(ResxFlowPage.FILTER);
            clearButton.Content = Resx.GetLabel(ResxFlowPage.CLEAR);
        }

        public void Prepare(GridViewBoundColumnBase ColumnGrid)
        {
            column = ColumnGrid;

            if (m_oFilter == null)
            {
                CreateFilters();
            }

            if (m_oFilter != null) m_oFilter.Value = m_oSelectedValue;
        }

        private void CreateFilters()
        {
            string dataMember = column.DataMemberBinding.Path.Path;
            m_oFilter = new Telerik.Windows.Data.FilterDescriptor(dataMember
                , Telerik.Windows.Data.FilterOperator.IsEqualTo
                , null);
        }

        private void OnClear(object sender, RoutedEventArgs e)
        {
            if (column.DataControl.FilterDescriptors.Contains(m_oFilter))
            {
                column.DataControl.FilterDescriptors.Remove(m_oFilter);
            }

            IsActive = false;
            rRead.IsChecked = false;
            rNotRead.IsChecked = false;
        }

        private void OnFilter(object sender, RoutedEventArgs e)
        {
            if ((rRead.IsChecked == false) && (rNotRead.IsChecked == false))
            {
                return;
            }
            m_oFilter.Value = m_oSelectedValue;

            if (!column.DataControl.FilterDescriptors.Contains(m_oFilter))
            {
                column.DataControl.FilterDescriptors.Add(m_oFilter);
            }

            IsActive = true;
        }

        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton l_oSelectedRadio = (RadioButton)sender;

            m_oSelectedValue = l_oSelectedRadio.Name.Equals("rRead") ? 1 : 0;
        }

        #endregion Methods


        public void Prepare(GridViewColumn ColumnGrid)
        {
            //column = ColumnGrid;

            //if (m_oFilter == null)
            //{
            //    CreateFilters();
            //}

            //if (m_oFilter != null) m_oFilter.Value = m_oSelectedValue;
        }
    }
}